package com.example.home.bus.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.bus.activity.BusDetail;
import com.example.home.bus.bean.BusListBean;
import com.google.gson.Gson;

import java.util.List;

public class BusListAdapter extends RecyclerView.Adapter<BusListAdapter.MyHoldy> {

    private Context context;
    private List<BusListBean.RowsBean> list;

    public BusListAdapter(Context context, List<BusListBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public BusListAdapter.MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_bus,null);
        return new BusListAdapter.MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BusListAdapter.MyHoldy holder, int position) {
        holder.name.setText(list.get(position).getName());
        holder.first.setText("起:"+list.get(position).getFirst());
        holder.end.setText("终:"+list.get(position).getEnd());
        holder.startTime.setText("首:"+list.get(position).getStartTime());
        holder.endTime.setText("末:"+list.get(position).getEndTime());
        holder.price.setText(String.valueOf(list.get(position).getPrice())+"元");
        holder.mileage.setText(list.get(position).getMileage()+"km");
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, BusDetail.class);
                Gson gson = new Gson();
                intent.putExtra("busId",gson.toJson(list.get(position)));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        private TextView name;
        private TextView first;
        private TextView end;
        private TextView startTime;
        private TextView endTime;
        private TextView price;
        private TextView mileage;
        private LinearLayout rootView;

        public MyHoldy(@NonNull View itemView) {
            super(itemView);

            name = itemView.findViewById(R.id.name);
            first = itemView.findViewById(R.id.first);
            end = itemView.findViewById(R.id.end);
            startTime = itemView.findViewById(R.id.startTime);
            endTime = itemView.findViewById(R.id.endTime);
            price = itemView.findViewById(R.id.price);
            mileage = itemView.findViewById(R.id.mileage);
            rootView = itemView.findViewById(R.id.rootView);
        }
    }
}
